import {Component, OnInit, ViewContainerRef} from '@angular/core';
import {NzTreeVo} from "../../../../vo/NzTreeVo";
import {Member} from "../../../../model/Member";
import {ManageService} from "../../../../services/manage.service";
import {AlertService} from "../../../../services/alert.service";
import {NzContextMenuService, NzDropdownMenuComponent} from "ng-zorro-antd/dropdown";
import {NzModalService} from "ng-zorro-antd/modal";
import {AuthService} from "../../../../services/auth.service";
import {BmeditComponent} from "../bmedit/bmedit.component";
import {GlyeditComponent} from "../glyedit/glyedit.component";
import {NzFormatEmitEvent} from "ng-zorro-antd/core/tree";

@Component({
  selector: 'app-glylist',
  templateUrl: './glylist.component.html',
  styleUrls: ['./glylist.component.css']
})
export class GlylistComponent implements OnInit {
  dl: Array<NzTreeVo>
  al: Array<Member>;
  activatedNode: any;
  pid: string;
  isshowadd = false;

  constructor(private manageService: ManageService,
              private alertService: AlertService,
              private nzContextMenuService: NzContextMenuService,
              private modal: NzModalService,
              private viewContainerRef: ViewContainerRef) {
    this.dl = new Array<NzTreeVo>()
    this.al = new Array<Member>()
  }

  ngOnInit(): void {
    this.loadDeptTree()
  }


  loadDeptTree() {
    this.manageService.getDeptNzTree()
      .then((data: any) => {
        if (data.msg == 'ok') {
          this.dl = new Array<NzTreeVo>();
          this.dl = data.list;
        }
        else {
          this.alertService.showError('机构信息获取异常，请重试');
        }
      })
  }

  onClickNode(data: NzFormatEmitEvent) {
    this.activatedNode = data.node!;
    this.getAdminList();
  }

  getAdminList() {
    this.manageService.getMemberListByDeptid(this.activatedNode.key)
      .then((data: any) => {
        if (data.msg == 'ok') {
          this.al = new Array<Member>();
          this.al = data.list;
        }
        else if (data.msg == 'empty') {
          this.al = new Array<Member>();
        }
        else {
          this.alertService.showError('获取管理员信息异常，请重试');
        }
      })
  }

  contextMenu($event: MouseEvent, menu: NzDropdownMenuComponent, key: string): void {

    this.pid = key;
    //console.log(key);
    if (key == "ROOT") {
      this.isshowadd = true;
    }
    else {
      this.isshowadd = false;
    }
    this.nzContextMenuService.create($event, menu);

  }


  add() {
    const modalwindow = this.modal.create({
      nzTitle: '部门信息编辑',
      nzContent: BmeditComponent,
      nzViewContainerRef: this.viewContainerRef,
      nzComponentParams: {
        optype: 'add',
        pid: "ROOT"
      },

      nzOnOk: () => new Promise(resolve => {
        // modalwindow.getContentComponent().save();
        modalwindow.getContentComponent().save();
        setTimeout(() => {

          resolve(true);
          this.loadDeptTree();
        }, 2000)
      })
    })
  }

  update() {
    const modalwindow = this.modal.create({
      nzTitle: '部门信息编辑',
      nzContent: BmeditComponent,
      nzViewContainerRef: this.viewContainerRef,
      nzComponentParams: {
        optype: 'update',
        id: this.pid
      },

      nzOnOk: () => new Promise(resolve => {
        // modalwindow.getContentComponent().save();
        modalwindow.getContentComponent().save();
        setTimeout(() => {

          resolve(true);
          this.loadDeptTree();
        }, 2000)
      })
    })
  }

  delete() {
    this.manageService.deleteDept(this.pid)
      .then((data: any) => {
        if (data.msg == 'ok') {
          this.loadDeptTree();
        }
        else {
          this.alertService.showError('删除异常，请重试');
        }
      })
  }

  updateAdmin(adminid: string) {

    const modalwindow = this.modal.create({
      nzTitle: '管理员信息编辑',
      nzContent: GlyeditComponent,
      nzViewContainerRef: this.viewContainerRef,
      nzComponentParams: {
        optype: 'update',
        deptid: this.activatedNode.key,
        adminid: adminid
      },

      nzOnOk: () => new Promise(resolve => {
        let temp = modalwindow.getContentComponent().a;
        if (!temp.name) {
          this.alertService.showWarning("用户名必填，请输入");
          resolve(false);
        }
        else if (!temp.pass) {
          this.alertService.showWarning('密码必填，请输入')
          resolve(false);
        }
        else if (!temp.rolegroup) {
          this.alertService.showWarning('岗位必须选择');
          resolve(false);
        }
        else if (!temp.state) {
          this.alertService.showWarning("状态必须选择");
          resolve(false);
        } else {
          modalwindow.getContentComponent().update();
          setTimeout(() => {

            resolve(true);
            this.getAdminList();
          }, 2000)
        }


      })
    })


  }


  deleteAdmin(adminid: string) {

    this.manageService.deleteMember(adminid)
      .then((data: any) => {
        if (data.msg == 'ok') {
          this.getAdminList();
        }
        else {
          this.alertService.showError('删除管理员异常，请重试');
        }
      })


  }

  addAdmin() {

    const modalwindow = this.modal.create({
      nzTitle: '管理员信息编辑',
      nzContent: GlyeditComponent,
      nzViewContainerRef: this.viewContainerRef,
      nzComponentParams: {
        optype: 'add',
        deptid: this.activatedNode.key
      },

      nzOnOk: () => new Promise(resolve => {
        let temp = modalwindow.getContentComponent().a;
        if (!temp.name) {
          this.alertService.showWarning("用户名必填，请输入");
          resolve(false);
        }
        else if (!temp.pass) {
          this.alertService.showWarning('密码必填，请输入')
          resolve(false);
        }
        else if (!temp.rolegroup) {
          this.alertService.showWarning('岗位必须选择');
          resolve(false);
        }
        else if (!temp.state) {
          this.alertService.showWarning("状态必须选择");
          resolve(false);
        } else {
          modalwindow.getContentComponent().save();
          setTimeout(() => {

            resolve(true);
            this.getAdminList();
          }, 2000)
        }


      })
    })

  }










}
